<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/written_language.php');
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/written_language_name_translation.php');
class writtenLanguageAction
{
	private $written_language;
	private $written_language_name_translation;
	function __construct()
	{
		$this->written_language = new written_language();
		$this->written_language_name_translation = new written_language_name_translation();
	}
	/*public function getWrittenLanguageTagAndName($user_lang='zh-Hans-CN')
	{
		global $dbh;
		$sql_1 = "SELECT `id`,`tag` FROM `written_language`";
		$id_name = array();
		$result_1 = $dbh->query($sql_1);
		$result_1->setFetchMode(PDO::FETCH_ASSOC);
		$id_tag = $result_1->fetchAll();
		$sql_2 = "SELECT `written_language_id`,`".$user_lang."` FROM `written_language_name_translation`";
		$result_2 = $dbh->query($sql_2);
		$result_2->setFetchMode(PDO::FETCH_ASSOC);
		$id_name = $result_2->fetchAll();
		foreach($id_tag as $one_id_tag)
		{
			foreach($id_name as $one_id_name)
			{
				if($one_id_name['written_language_id']==$one_id_tag['id'])
				{
					$tag_name[$one_id_tag['tag']] = $one_id_name[$user_lang];
				}
			}
	}
	return $tag_name;
	}*/

	public function getWrittenLanguageTagAndName($tag='zh-Hans-CN')
	{
		try
		{
			$id_tag = $this->written_language->getIdAndTag();
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
		try
		{
			$id_name = $this->written_language_name_translation->getIdAndName($tag);
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
				//print_r($id_tag);
		//print_r($id_name);
		foreach($id_tag as $id=>$one_tag)
		{
			$tag_name[$one_tag] = $id_name[$id][$tag];
		}
		return $tag_name;
	}
	
	public function getWrittenLanguageIdAndName($tag='zh-Hans-CN')
	{
		try
		{
			$id_tag = $this->written_language->getIdAndTag();
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
		try
		{
			$id_name = $this->written_language_name_translation->getIdAndNameByTag($tag);
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		foreach($id_tag as $id=>$one_tag)
		{
			$id_name[$id] = $id_name[$id]."[".$one_tag."]";
		}
		return $id_name;
	}

	public function getNameByIdSum($id_sum,$tag='zh-Hans-CN')
	{
		try
		{
			$id_name = $this->written_language_name_translation->getIdAndName($tag);
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		foreach($id_name as $id=>$one_name)
		{
			$a = $id_sum&$id;
			if($a!=0)
			{
				$name[] = $one_name;
			}
		}
		return $name;
	}

	public function getOriginalNameByIdSum($id_sum)
	{
		try
		{
			if(empty($id_sum))
			{
				return array();
			}
			$id_tag = $this->written_language->getIdAndTag();
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
		try
		{
			$id_all_name = $this->written_language_name_translation->getIdAndName();
			//print_r($id_all_name);
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
		foreach($id_tag as $id=>$one_tag)
		{
			$id_name[$id] = $id_all_name[$id][$one_tag];
		}

		foreach($id_name as $id=>$one_name)
		{
			$a = $id_sum&$id;
			if($a!=0)
			{
				$name[] = $one_name;
			}
		}
		
		return $name;
	}
}

?>
